1 package edu.jiangxin.apktoolbox.android.screenshot;
2
3 import edu.jiangxin.apktoolbox.swing.extend.EasyPanel;
4 import edu.jiangxin.apktoolbox.utils.Constants;
5 import edu.jiangxin.apktoolbox.utils.DateUtils;
6 import edu.jiangxin.apktoolbox.utils.Utils;
7 import org.apache.commons.lang3.StringUtils;
8
9 import javax.imageio.ImageIO;
10 import javax.swing.*;
11 import java.awt.*;
12 import java.awt.datatransfer.DataFlavor;
13 import java.awt.datatransfer.Transferable;
14 import java.awt.datatransfer.UnsupportedFlavorException;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.ActionListener;
17 import java.io.File;
18 import java.io.IOException;
19
20
21
22
23
24
25 public class ScreenShotPanel extends EasyPanel {
26 private static final long serialVersionUID = 1L;
27
28 private JTextField directoryTextField;
29
30 private JTextField fileNameTextField;
31
32 private JCheckBox openCheckBox;
33
34 private JCheckBox copyCheckBox;
35
36 public ScreenShotPanel() throws HeadlessException {
37 super();
38 }
39
40 @Override
41 public void initUI() {
42 BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
43 setLayout(boxLayout);
44
45 createDirectoryPanel();
46 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
47 createFileNamePanel();
48 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
49 createScreenshotPanel();
50 }
51
52 private void createScreenshotPanel() {
53 JPanel screenshotPanel = new JPanel();
54 screenshotPanel.setLayout(new BoxLayout(screenshotPanel, BoxLayout.X_AXIS));
55 add(screenshotPanel);
56
57 openCheckBox = new JCheckBox("Open Dir");
58 openCheckBox.setSelected(false);
59
60 copyCheckBox = new JCheckBox("Copy Pic");
61 copyCheckBox.setSelected(false);
62
63 JButton screenshotButton = new JButton("Sceenshot");
64 screenshotButton.addActionListener(new ScreenshotButtonActionListener());
65
66 JButton getExistButton = new JButton("Get Exist");
67 getExistButton.addActionListener(new GetExistButtonActionListener());
68
69 screenshotPanel.add(openCheckBox);
70 screenshotPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
71 screenshotPanel.add(copyCheckBox);
72 screenshotPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
73 screenshotPanel.add(screenshotButton);
74 screenshotPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
75 screenshotPanel.add(getExistButton);
76 }
77
78 private void createFileNamePanel() {
79 JPanel fileNamePanel = new JPanel();
80 fileNamePanel.setLayout(new BoxLayout(fileNamePanel, BoxLayout.X_AXIS));
81 add(fileNamePanel);
82
83 fileNameTextField = new JTextField();
84 fileNameTextField.setToolTipText("timestamp default(for example: 20180101122345.png)");
85
86 JButton fileNameButton = new JButton("File name");
87
88 fileNamePanel.add(fileNameTextField);
89 fileNamePanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
90 fileNamePanel.add(fileNameButton);
91 }
92
93 private void createDirectoryPanel() {
94 JPanel directoryPanel = new JPanel();
95 directoryPanel.setLayout(new BoxLayout(directoryPanel, BoxLayout.X_AXIS));
96 add(directoryPanel);
97
98 directoryTextField = new JTextField();
99 directoryTextField.setText(conf.getString("screenshot.save.dir", System.getenv("USERPROFILE")));
100 directoryTextField.setTransferHandler(new DirectoryTextFieldTransferHandler());
101
102 JButton directoryButton = new JButton("Save Directory");
103 directoryButton.addActionListener(new DirectoryButtonActionListener());
104
105 directoryPanel.add(directoryTextField);
106 directoryPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
107 directoryPanel.add(directoryButton);
108 }
109
110 private final class DirectoryButtonActionListener implements ActionListener {
111 @Override
112 public void actionPerformed(ActionEvent e) {
113 JFileChooser jfc = new JFileChooser(directoryTextField.getText());
114 jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
115 jfc.setDialogTitle("select a directory");
116 int ret = jfc.showDialog(new JLabel(), null);
117 switch (ret) {
118 case JFileChooser.APPROVE_OPTION:
119 File file = jfc.getSelectedFile();
120 directoryTextField.setText(file.getAbsolutePath());
121 conf.setProperty("screenshot.save.dir", file.getAbsolutePath());
122 break;
123
124 default:
125 break;
126 }
127
128 }
129 }
130
131 private final class GetExistButtonActionListener implements ActionListener {
132 @Override
133 public void actionPerformed(ActionEvent e) {
134 System.out.println("Get Exist");
135 }
136 }
137
138 private final class ScreenshotButtonActionListener implements ActionListener {
139 @Override
140 public void actionPerformed(ActionEvent e) {
141 String title = Utils.getFrameTitle(ScreenShotPanel.this);
142 Utils.setFrameTitle(ScreenShotPanel.this, title + " [Processing]");
143 String dirName = fileNameTextField.getText();
144 if (StringUtils.isEmpty(dirName)) {
145 String defaultDir = System.getenv("USERPROFILE");
146 dirName = conf.getString("screenshot.save.dir", defaultDir);
147 logger.info("dirName: " + dirName);
148 }
149 String fileName = fileNameTextField.getText();
150 if (StringUtils.isEmpty(fileName)) {
151 fileName = DateUtils.getCurrentDateString() + ".png";
152 }
153 File file = new File(dirName, fileName);
154 try {
155 Utils.blockedExecutor("adb shell /system/bin/screencap -p /sdcard/screenshot.png");
156 logger.info("screencap finish");
157 Utils.blockedExecutor("adb pull /sdcard/screenshot.png " + file.getCanonicalPath());
158 logger.info("pull finish");
159 if (openCheckBox.isSelected()) {
160 Utils.blockedExecutor("explorer /e,/select, " + file.getCanonicalPath());
161 logger.info("open dir finish");
162 }
163 if (copyCheckBox.isSelected()) {
164 logger.info("copy the snapshot");
165 Image image = ImageIO.read(file);
166 setClipboardImage(ScreenShotPanel.this, image);
167 logger.info("copy finish");
168 }
169 } catch (IOException e1) {
170 logger.error("screenshot fail", e1);
171 } finally {
172 Utils.setFrameTitle(ScreenShotPanel.this, title);
173 }
174 }
175 }
176
177 private final class DirectoryTextFieldTransferHandler extends TransferHandler {
178 private static final long serialVersionUID = 1L;
179
180 @Override
181 public boolean importData(JComponent comp, Transferable t) {
182 try {
183 Object o = t.getTransferData(DataFlavor.javaFileListFlavor);
184 String filepath = o.toString();
185 filepath = filepath.substring(1, filepath.length() - 1);
186 directoryTextField.setText(filepath);
187 return true;
188 } catch (Exception e) {
189 logger.error("import data excetion", e);
190 }
191 return false;
192 }
193
194 @Override
195 public boolean canImport(JComponent jComponent, DataFlavor[] dataFlavors) {
196 for (int i = 0; i < dataFlavors.length; i++) {
197 if (DataFlavor.javaFileListFlavor.equals(dataFlavors[i])) {
198 return true;
199 }
200 }
201 return false;
202 }
203 }
204
205 private static void setClipboardImage(JPanel frame, final Image image) {
206 Transferable trans = new Transferable() {
207 @Override
208 public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
209 if (isDataFlavorSupported(flavor)) {
210 return image;
211 }
212 throw new UnsupportedFlavorException(flavor);
213 }
214
215 @Override
216 public DataFlavor[] getTransferDataFlavors() {
217 return new DataFlavor[] { DataFlavor.imageFlavor };
218 }
219
220 @Override
221 public boolean isDataFlavorSupported(DataFlavor flavor) {
222 return DataFlavor.imageFlavor.equals(flavor);
223 }
224 };
225
226 frame.getToolkit().getSystemClipboard().setContents(trans, null);
227 }
228 }